<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :default-active="onRoutes"
      :collapse="collapse"
      background-color="#324157"
      text-color="#bfcbd9"
      active-text-color="#20a0ff"
      unique-opened
      router
    >
      <div v-for="(item,index) in items"  :key="index">
          <template v-if="item.children && item.children.length>0 ">
            <el-submenu :index="item.name" :key="index">
              <template slot="title">
                <i class="el-icon-s-order"></i>
                <span slot="title">{{ item.title }}</span>
              </template>
              <template v-for="(subItem,idx) in item.children">
                <el-submenu
                  v-if="subItem.children && subItem.children.length"
                  :index="subItem.name"
                  :key="idx"
                >
                  <template slot="title">{{ subItem.title }}</template>
                  <el-menu-item
                    v-for="(threeItem,i) in subItem.children"
                    :key="i"
                    :index="threeItem.name"
                  >{{ threeItem.title }}</el-menu-item>
                </el-submenu>
                <el-menu-item
                  v-else
                  :index="subItem.title"
                  :key="subItem.index"
                >{{ subItem.title }}</el-menu-item>
              </template>
            </el-submenu>
          </template>
          <template v-else>
            <el-menu-item :index="item.index" :key="item.index">
              <i :class="item.icon"></i>
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>
          </template>
      </div>
    </el-menu>
  </div>
</template>

<script>
import bus from '../common/bus';
import {slideData} from '../../utils/menuConfig.js';
export default {
  data () {
    return {
      collapse: false,
      items: slideData,
    }
  },
  computed: {
    onRoutes () {
      console.log(this.$route)
      return this.$route.name
    }
  },
  created () {

    let menuData = JSON.parse(localStorage.getItem('userInfo'));
    // this.items = this.handleMenuData(menuData.roles[0].resources);
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    bus.$on('collapse', msg => {
      this.collapse = msg;
    })
    // this.$store.dispatch('initMenuList')
  },
  methods: {
    //处理菜单数据，为了侧边栏好跳转路由
    handleMenuData (data) {
      data.forEach(item => {
        if (item.children && item.children.length) {
          item.children.forEach(child => {
            child.title = child.name.split(':')[1];
          })
        }
      })
      return data;
    },
  },
  watch: {
  }
}
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 44px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 220px;
}
.sidebar > ul {
  height: 100%;
}
.el-menu-item,
.el-submenu__title {
  height: 40px;
  line-height: 40px;
}

.hover-menu-item {
  height: 44px;
  line-height: 44px;
}
.el-submenu__title {
  height: 40px;
  line-height: 40px;
}
</style>
<style>
.el-menu-item,
.el-submenu__title {
  height: 40px;
  line-height: 40px;
}

.hover-menu-item {
  height: 44px;
  line-height: 44px;
}
</style>

